<?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

class Test_Question_Model extends MY_Model {
	protected $_table_name = 'test_question';

	public function __construct() {
		parent::__construct();
	}	
	
	public function get_questions($test_id)
	{
		$this->db->where('test_id',$test_id);
		$this->db->join('questions_detail','questions_detail.id = question_detail_id');
		$rs = $this->db->get($this->_table_name)->result_array();
		//echo $this->db->last_query();
		return $rs;
	}
	
	public function get_total_qty($question_id, $percentage, $total_qty)
	{
		$hasil = ($percentage * $total_qty) / 100;
		$this->db->where('question_id', $question_id);
		$this->db->from('questions_detail');
		$total = $this->db->count_all_results();
		
		//echo "hasil = " . $hasil . "<br>";
		//echo $this->db->last_query() . " " . $total;
		
		if($total >= round($hasil))
			//echo "if";
			return $total;
		else
			//echo "else";
			return -1;
	}
	
	public function insert($last_test_id, $question_id, $percentage, $total_qty) {
		$this->delete($last_test_id); //pastikan tidak ada data sejenis agar tidak terjadi duplikat isi
		$counter=0;
		$temp = $total_qty;
		$question_id = explode(",",$question_id);
		$percentage = explode(",", $percentage);
		
		for($a=0; $a< count($question_id); $a++)
		{
			$hasil = ($percentage[$a] * $total_qty) / 100;
			if($temp - round($hasil) ==0 && $a < (count($question_id)-1) ) //jika hasil perhitungan menghabiskan sisa jatah soal untuk bab berikutnya
				if(round($hasil) >1)
					$hasil = round($hasil) - 1; //agar sub bab lainnya dapat ditarik soalnya, subbab sekarang jatah soalnya - 1.
			
			$temp = $temp - round($hasil);
			
			$this->db->where('question_id', $question_id[$a]);
			$this->db->order_by("","random");
			$this->db->limit( round($hasil)) ;
			$rs = $this->db->get('questions_detail')->result_array();
			//echo "Jumlah soal berdasarkan prosentase = (" . $percentage[$a] . "*" . $total_qty . ") / 100 = " . $hasil . " ==> " .round($hasil) . "<Br>";
			//echo $this->db->last_query() . " <Br>";
			
			foreach($rs as $row)
			{
				$data['test_id'] = $last_test_id;
				$data['question_detail_id'] = $row['id'];
				if($counter<$total_qty) {
					$this->db->insert($this->_table_name, $data);
					$counter++;
					//echo $this->db->last_query() . " <br>";
				}
			}
			
		}
		
	}
	
	public function delete($id){
		$this->db->delete($this->_table_name, array('test_id'=>$id));
	}
	
	/*
	public function lists_array(){
		$rs = $this->db->get($this->_table_name)->result_array();
		$data = array();
		$data[0] = '';
		foreach($rs as $row){
			$data[ $row['id'] ] = $row['username'];
		}
		return $data;
	}
	
	public function get($id='') {
		$user = parent::get($id);
		return $user;
	}

	public function get_name($id) {
		$this->db->where('id',$id);
		$rs = $this->db->get($this->_table_name);
		$rs = $rs->result_array();
		return $rs[0]["nama"];
	}
	
	public function update(&$input, $id) {
		$user = $this->get($id);
		$data['username'] = $user['username'];
		$data['nama'] = $input->post('nama');
		$pass = $input->post('password');
		if($pass!='')
			$data['password'] = md5($pass);
		else
			$data['password'] = $user['password'];
		
		$data['email'] = $input->post('email');
		$this->db->update($this->_table_name, $data, array('id'=>$user['id']));
	}
	
	public function is_valid_to_delete($user_id){
		$ci = &get_instance();
		
		$ci->load->model('prospeklog_model','prospeklog');
		$prospeklog = $ci->prospeklog->get_by_user_id($user_id);
		if(count($prospeklog)>0)
			return false;
				
		return true;
	}
	
	

	public function change_password($user_id, $new_password) {
		$this->db->update($this->_table_name, array('password'=>$this->_encrypt($new_password)), array('id'=>$user_id));
	}

	public function encrypt_password($password) {
		return $this->_encrypt($password);
	}
	*/
}
